package com.huo.qq.pengyou;

import java.util.Map;

import javax.swing.JLabel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.huo.common.StringUtil;
import com.huo.http.HttpService;
import com.huo.qq.common.GlobalURL;
import com.huo.qq.common.QQUtils;
import com.huo.qq.common.Regulars;

/**
 * Copyright (c) 2013 by Huoyan.
 * <p>Title: 朋友网客户端</p>
 * <p>Description: </p>
 * @author Huoyan
 * @version 1.0
 */
public class FriendClient extends HttpService {
	
	private final Log log = LogFactory.getLog(this.getClass());
	
	private String qq = null;
	private String pwd = null;
	private String loginSign = "";
	
	//private long clientid = 18662986;
	
	public FriendClient(String qq, String pwd) {
		super();
		this.qq = qq;
		this.pwd = pwd;
	}

	public FriendClient(String qq, String pwd, long clientid) {
		super();
		this.qq = qq;
		this.pwd = pwd;
		//this.clientid = clientid;
	}
	
	/**
	 * 验证是否需要验证码登录. 如需,则获取.
	 * @param qq
	 * @param lbl
	 * @return
	 */
	public String getVerifyCode(String qq, JLabel lbl){
		//登录步骤1,获取登陆表单信息，检查当前QQ是否需要验证码登录
		StringBuffer checkIdUrl = new StringBuffer(GlobalURL.CHECK_ID_URL_FRIEND);
		checkIdUrl.append("&appid=").append(GlobalURL.APPID_FRIEND)
					.append("&uin=").append(qq)
					.append("&r=").append(QQUtils.getRandom());
		
		//发送请求
		String res = getReturnStr(checkIdUrl.toString());
		//使用正则匹配获取验证码编号
		// ptui_checkVC('0','!ZLE');返回这个就不需要获取验证码了。验证码就是!ZLE
		// ptui_checkVC('1','95ab7db15e5ab17f50f25d33598259e83ccc098c4af2f8a4');这个长字符串需要验证码登录
		String checkType = StringUtil.findPattern(Regulars.CHECK_NEED_FRIEND, res);
		
		String check = ""; //客户端输入的验证码
		//判断是否需要验证码登录
		if (!checkType.startsWith("!")) {
			//需要输入验证码登录
//			byte[] ibs = readCheckImage(GlobalURL.CHECK_IMAGE_URL + "&uin=" + qq + "&vc_type=" + checkType);
//			//将图片设置在组件JLable上
//			ImageIcon ii = new ImageIcon(ibs);
//			lbl.setIcon(ii);
		}else{
			//无需验证码登录
			check = checkType;
		}
		return check;
	}
	
	/**
	 * 登录
	 * @param verifyCode
	 * @throws Exception
	 */
	public String login(String verifyCode) throws Exception{
		
		//组装登录URL
		StringBuffer loginUrl = new StringBuffer(GlobalURL.LOGIN_URL_FRIEND);
		loginUrl.append("&u=").append(qq)
				.append("&p=").append(QQUtils.encodePass(pwd, verifyCode, QQUtils.uin2hex(qq)))
				.append("&verifycode=").append(verifyCode)
				.append("&aid=").append(GlobalURL.APPID_FRIEND);
		
		//发送请求-登录
		log.info(loginUrl.toString());
		String res = getReturnStr(loginUrl.toString());
		log.info(res);
		
		/**
		 * ptuiCB('24','0','','0','很遗憾，网络连接出现异常，请您检查是否禁用cookies。(2326791224)', '51784370');
		 * ptuiCB('0','0','http://www.pengyou.com/index.php?mod=login2&act=qqlogin','1','登录成功！', '[昵称]');
		*/
		if (StringUtil.exist("登录成功", res)) {
			log.info(">>登录成功..");
			return "登陆成功";
		} else {
			log.info("一登录失败");
			//System.out.println(res.indexOf("3",8)); //待完善
			return res;
		}
	}	
	
	public static void main(String[] args) {
		FriendClient fc = new  FriendClient("51784370", "asdf1234");
		String code = fc.getVerifyCode("51784370", null);
		try {
			fc.login(code);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
